export function findName(options, value) {
	var name = "";
	for (let i = 0; i < options.length; i++) {
		if (options[i].value == value) {
			name = options[i].label || options[i].text;
		}
	}
	return name || value;
}
export function findTreeName(options, value) {
	var name3 = "";

	function Name(name) {
		this.name = name;
	}
	var name1 = new Name("");
	forfn(options, value, name1);
	return name1.name;
}

function forfn(options, value, name1) {
	function changeName(n1, x) {
		n1.name = x;
	}
	for (let i = 0; i < options.length; i++) {
		if (options[i].id == value) {
			changeName(name1, options[i].label || options[i].text);
		}
		if (options[i].children) {
			forfn(options[i].children, value, name1);
		}
	}
}
export function locationTree(arr, type) {
	arr.forEach((item) => {
		item.id = item[type ?? "id"];
		item.value = item[type ?? "id"];
		item.label = item.deptName;
		item.isDisabled = item.locationFlag == "N" ? true : false;
		if (item.children && item.children.length > 0) {
			locationTree(item.children, type);
		}
	});
	return JSON.parse(JSON.stringify(arr));
}
// ! 提供导出列表字段
export function convertToDefaultObject(columns) {
	const defaultObject = {};

	columns.forEach((column) => {
		if (column.prop) {
			defaultObject[column.prop] = null;
		}
	});

	return defaultObject;
}
export function calculateYearsSinceInstallation(installationDateStr) {
	if (!installationDateStr) return null;
	// 将安装日期字符串转换为 Date 对象
	const installationDate = new Date(installationDateStr);

	// 获取当前日期
	const currentDate = new Date();

	// 检查安装日期是否有效
	if (isNaN(installationDate.getTime())) {
		console.error("Invalid installation date provided.");
		return null;
	}

	// 计算两个日期之间的毫秒数差异
	const diffInMilliseconds = currentDate - installationDate;

	// 将毫秒数转换为年数
	// 一年平均有 365.25 天，考虑到闰年
	const years = diffInMilliseconds / (1000 * 60 * 60 * 24 * 365.25);

	// 返回年数，保留两位小数
	return parseFloat(years.toFixed(2));
}
// 防抖函数
export function debounce(func, wait) {
	let timeout;
	return function() {
		const context = this;
		const args = arguments;
		clearTimeout(timeout);
		timeout = setTimeout(function() {
			func.apply(context, args);
		}, wait);
	};
}

// 节流函数
export function throttle(func, wait) {
	let timeout;
	return function() {
		const context = this;
		const args = arguments;
		if (!timeout) {
			timeout = setTimeout(function() {
				timeout = null;
				func.apply(context, args);
			}, wait);
		}
	};
}
// 递归获取树形结构
export function getTreeName(arr, label, value) {
	arr.forEach((item) => {
		item.value = item[value];
		item.text = item[label];
		if (item.children && item.children.length > 0) {
			getTreeName(item.children, label, value);
		}
	});
	return arr;
}

// 判断文件是否可以预览
export function canViewFile(filePath) {
	// 获取文件扩展名
	const ext = filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase();

	// 定义可查看文件的扩展名列表
	const viewableExtensions = [
		"jpg",
		"jpeg",
		"png",
		"gif",
		"pdf",
		"doc",
		"docx",
		"xls",
		"xlsx",
		"ppt",
		"pptx",
		"txt",
		"md",
		"mp4",
		"webm",
		"ogg",
	];
	const pictureViewableExtensions = ["jpg", "jpeg", "png", "gif", "bmp"];
	const videoViewableExtensions = ["mp4", "webm", "ogg"];
	const documentViewableExtensions = [
		"pdf",
		"doc",
		"docx",
		"xls",
		"xlsx",
		"ppt",
		"pptx",
		"txt",
		"md",
	];
	// 判断文件是否可查看
	const canView = viewableExtensions.includes(ext);
	if (canView) {
		const fileUrl = getApp().globalData.config.baseUrl + filePath;
		console.log("文件可以查看", ext, fileUrl);
		if (videoViewableExtensions.includes(ext)) {
			return fileUrl;
		}
		if (pictureViewableExtensions.includes(ext)) {
			uni.previewImage({
				current: 0,
				urls: [fileUrl],
			});
		} else if (documentViewableExtensions.includes(ext)) {
			uni.downloadFile({
				url: fileUrl,
				success: function(res) {
					const filePath = res.tempFilePath;
					uni.openDocument({
						filePath: filePath,
						showMenu: true,
						success: function(res) {
							console.log("打开文档成功");
						},
					});
				},
			});
		}
		// 在这里执行查看文件的操作，例如使用 uni.previewImage 预览图片，uni.openDocument 打开文档等
	} else {
		console.log("文件不可以查看");
		// 处理不可查看的文件，比如提示用户或者转换文件格式
	}
}

// 将秒转换为x天x时x分x秒
export function convertSecondsToFormat(seconds) {
	var days = Math.floor(seconds / (3600 * 24));
	var hours = Math.floor((seconds % (3600 * 24)) / 3600);
	var mins = Math.floor((seconds % 3600) / 60);
	var sec = seconds % 60;

	let format = "";
	if (days > 0) {
		format += days + "天";
	}
	if (hours > 0) {
		format += hours + "小时";
	}
	if (mins > 0) {
		format += mins + "分钟";
	}
	format += sec + "秒";

	return format;
}

// 检查网络状态
export function checkNetwork() {
	return new Promise((resolve) => {
		uni.getNetworkType({
			success: (res) => {
				resolve(
					res.networkType === 'none' ?
					'disconnected' :
					'connected',
				);
			},
			fail: () => resolve('disconnected'),
		});
	});
}